package com.iemes.controller.finance;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.FineFormMap;
import com.iemes.entity.InvoiceFormMap;
import com.iemes.entity.PcontractTrackingDetailFormMap;
import com.iemes.entity.ReceivablesFormMap;
import com.iemes.mapper.finance.ReceivablesMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/finance")
@Controller
public class ReceivablesController extends BaseController {

	@Inject
	private ReceivablesMapper receivablesMapper;
	
	@RequestMapping("receivables")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/finance/receivablesmanagement/receivables_list";
	}
	
	@ResponseBody
	@RequestMapping("receivables_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		
		ReceivablesFormMap receivablesFormMap=getFormMap(ReceivablesFormMap.class);
		List<Map<String,Object>> receivablesFormMaps=receivablesMapper.getReceivablesByCondition(receivablesFormMap);
		for(Map<String,Object>map:receivablesFormMaps){
			//订单的单价
			double oprice=Double.parseDouble(map.get("oprice").toString());
			//税率
			double taxRate=Double.parseDouble(map.get("taxRate").toString());
			
			//将单价存进对账单
			map.put("priceExcludingTax", oprice);
			//含税单价
			map.put("unitPriceTaxIncluded", (double) Math.round((1+taxRate)*oprice * 100) / 100);
			
			//数量
			int many=Integer.parseInt(map.get("actualDelivery").toString());
			
			//含税单价
			double money=Double.parseDouble(map.get("unitPriceTaxIncluded").toString());
			//含税金额
			map.put("aprice", money*many);
		}
		LuogicPaging(pageNow, pageSize, receivablesFormMaps);
		return pageView;
	}
	
	@ResponseBody
	@RequestMapping(value="receivables_deleteEntity")
	@SystemLog(module="应收账款",methods="应收账款-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		ReceivablesFormMap receivablesFormMap=getFormMap(ReceivablesFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			receivablesFormMap.put("state", 1);
			receivablesFormMap.put("id", str[i]);
			baseMapper.editEntity(receivablesFormMap);
		}
		return "success";
	}
	
	@RequestMapping("receivables_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		ReceivablesFormMap receivablesFormMap=receivablesMapper.getReceivablesFormMapById(Integer.parseInt(id));
		model.addAttribute("receivablesFormMap", receivablesFormMap);
		return Common.BACKGROUND_PATH + "/finance/receivablesmanagement/receivables_edit";
	}
	
	@ResponseBody
	@RequestMapping(value="receivables_editEntity")
	@SystemLog(module="应收账款",methods="应收账款-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		ReceivablesFormMap receivablesFormMap=getFormMap(ReceivablesFormMap.class);
		baseMapper.editEntity(receivablesFormMap);
		return "success";
		
	}
	
	
	@RequestMapping("receivables_invoice_addUI")
	public String addInvoice(Model model) throws Exception {
		String id = getPara("id");
		//应收账款对象
		ReceivablesFormMap receivablesFormMap=receivablesMapper.getReceivablesFormMapById(Integer.parseInt(id));
		//获取开发票集合
		List<InvoiceFormMap> invoiceFormMaps=receivablesMapper.getInvoiceFormMapByReceivablesId(Integer.parseInt(id));
		
		model.addAttribute("invoiceFormMaps", invoiceFormMaps);
		model.addAttribute("receivablesFormMap", receivablesFormMap);
		return Common.BACKGROUND_PATH + "/finance/receivablesmanagement/receivables_add_invoice";
	}
	
	
	@ResponseBody
	@RequestMapping("receivables_invoice_deleteEntity")
	@SystemLog(module="发票",methods="发票-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String delInvoice(Model model) throws Exception {
		String id = getPara("pid");
		InvoiceFormMap invoiceFormMap=getFormMap(InvoiceFormMap.class);
		invoiceFormMap.put("state", 1);
		invoiceFormMap.put("id", id);
		baseMapper.editEntity(invoiceFormMap);
		return "success";
	}
	
	
	@ResponseBody
	@RequestMapping(value="receivables_invoice_editEntity")
	@SystemLog(module="发票",methods="发票-新增/修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String invoiceeditEntity() throws Exception {
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		ReceivablesFormMap receivablesFormMap=getFormMap(ReceivablesFormMap.class);
		receivablesMapper.editEntity(receivablesFormMap);
		
		//先删除
		baseMapper.deleteByAttribute("receivablesId","'"+ Integer.parseInt(receivablesFormMap.getStr("id")) +"'", InvoiceFormMap.class);
		
		//发票子项
		String[] itemid = request.getParameterValues("item_no_1");
		String[] itemBalanceUpArr = request.getParameterValues("item_balance_up_1");
		String[] itemUseNumberArr = request.getParameterValues("item_time");
		if(null != itemid ){
			for(int i=0;i<itemid.length;i++){
				InvoiceFormMap invoiceFormMap=new InvoiceFormMap();
				invoiceFormMap.put("receivablesId", Integer.parseInt(receivablesFormMap.getStr("id")));
				invoiceFormMap.put("customerName", itemid[i]);
				invoiceFormMap.put("invoiceNumber", itemBalanceUpArr[i]);
				invoiceFormMap.put("invoiceDate", itemUseNumberArr[i]);
				invoiceFormMap.put("state", 0);
				baseMapper.addEntity(invoiceFormMap);
			}
		}
		return "success";
		
		
	}
	
	
	
	@RequestMapping("receivables_fine_addUI")
	public String addFine(Model model) throws Exception {
		String id = getPara("id");
		//应收账款对象
		ReceivablesFormMap receivablesFormMap=receivablesMapper.getReceivablesFormMapById(Integer.parseInt(id));
		//获取开发票集合
		List<FineFormMap> fineFormMaps=receivablesMapper.getFineFormMapByReceivablesId(Integer.parseInt(id));
		
		model.addAttribute("fineFormMaps", fineFormMaps);
		model.addAttribute("receivablesFormMap", receivablesFormMap);
		return Common.BACKGROUND_PATH + "/finance/receivablesmanagement/receivables_add_fine";
	}
	
	
	@ResponseBody
	@RequestMapping("receivables_fine_deleteEntity")
	@SystemLog(module="罚款",methods="罚款-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String delFine(Model model) throws Exception {
		String id = getPara("pid");
		FineFormMap fineFormMap=getFormMap(FineFormMap.class);
		fineFormMap.put("state", 1);
		fineFormMap.put("id", id);
		baseMapper.editEntity(fineFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value="receivables_fine_editEntity")
	@SystemLog(module="罚款",methods="罚款-新增/修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String fineeditEntity() throws Exception {
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		ReceivablesFormMap receivablesFormMap=getFormMap(ReceivablesFormMap.class);
		receivablesMapper.editEntity(receivablesFormMap);
		
		//先删除
		baseMapper.deleteByAttribute("receivablesId","'"+ Integer.parseInt(receivablesFormMap.getStr("id")) +"'", FineFormMap.class);
		
		//罚款子项
		String[] itemid = request.getParameterValues("item_no_1");
		String[] itemBalanceUpArr = request.getParameterValues("item_balance_up_1");
		String[] itemUseNumberArr = request.getParameterValues("item_time");
		String[] itemBalanceDownArr = request.getParameterValues("item_balance_down_1");
		if(null != itemid ){
			for(int i=0;i<itemid.length;i++){
				FineFormMap fineFormMap=new FineFormMap();
				fineFormMap.put("receivablesId", Integer.parseInt(receivablesFormMap.getStr("id")));
				fineFormMap.put("contractNo", itemid[i]);
				fineFormMap.put("chargeTime", itemUseNumberArr[i]);
				fineFormMap.put("price", Double.parseDouble(itemBalanceUpArr[i]));
				fineFormMap.put("content", itemBalanceDownArr[i]);
				fineFormMap.put("state", 0);
				baseMapper.addEntity(fineFormMap);
			}
		}
		return "success";
	}
}
